package com.qf.j2112.controller;

import com.qf.j2112.po.SysMember;
import com.qf.j2112.utils.JwtUtil;
import com.qf.j2112.vo.AjaxResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.qf.j2112.service.SysMemberService;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@CrossOrigin
@Slf4j
public class SysMemberController {
    @Autowired
    private SysMemberService sysMemberService;

    /* 查询会员信息列表*/
    @RequestMapping(value = "/memberlist",method = RequestMethod.GET)
    public AjaxResult listmember() {
        List<SysMember> list = sysMemberService.selectSysMemberList();
        return AjaxResult.success("查询成功",list);
    }

    /*获取会员信息详细信息*/
    @RequestMapping(value = "/member/{memberId}",method = RequestMethod.GET)
    public AjaxResult getmemberInfo(@PathVariable("memberId") Integer memberId) {
        return AjaxResult.success(sysMemberService.selectSysMemberByMemberId(memberId));
    }

    /*获取会员信息详细信息*/
    @RequestMapping(value = "/member",method = RequestMethod.GET)
    public AjaxResult getmemberInfo2(HttpServletRequest request) {
        String token=request.getHeader("token");
        token= JwtUtil.getUsrInfoFromToken(token);
        Integer userId=Integer.valueOf(token);
        log.info("token被解析"+userId);
        SysMember member = sysMemberService.selectSysMemberByUserId(userId);
        return AjaxResult.success("查询成功",member);
    }

    /*新增会员信息*/
    @RequestMapping(value = "/addmember",method = RequestMethod.POST)
    public AjaxResult add(@RequestBody SysMember sysMember) {
        return AjaxResult.success(sysMemberService.insertSysMember(sysMember));
    }

    /*修改会员信息*/
    @RequestMapping(value = "/updatemember",method = RequestMethod.GET)
    public AjaxResult edit(@RequestBody SysMember sysMember) {
        return AjaxResult.success(sysMemberService.updateSysMember(sysMember));
    }

}
